package com.ez.supervise.action.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.lang.StringUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.base.BaseEntity;
import com.ez.supervise.model.biz.UserAddress;
import com.ez.supervise.service.UserAddressService;
import com.ez.supervise.util.ConvertUtil;
import com.ez.supervise.util.FreemarkerUtil;
import com.ez.supervise.util.JsonUtils;

public class UserAddressAction extends BaseJsonAction {

    private static final long serialVersionUID = 967358559670907830L;

    private UserAddressService userAddressService;

    private UserAddress userAddress;

    private Page page;

    private Long uerAddressId;
    
    private String ids;

    /**
     * 
     * @return
     */
    public String saveUserAddress() {
	try {
	    if (userAddress.isNew()) {
		userAddressService.insert(userAddress);
	    } else {
		userAddressService.update(userAddress);
	    }
	    Result result = new Result(userAddress.getId());
	    outJsonString(JSONObject.fromObject(result).toString());
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * 
     * @return
     */
    public String deleteUserAddress() {
	try {
	    if (uerAddressId != null && uerAddressId > 0) {
		userAddressService.delete(uerAddressId);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * 
     * @return
     * @throws Exception
     */
    public String findUserAddressByPage() throws Exception {
	page = new Page();
	String searchField = getRequest().getParameter("searchField");
	String searchValue = getRequest().getParameter("searchValue");
	if (StringUtils.isNotBlank(searchValue)) {
	    page.putCondition(searchField, searchValue.trim());
	}
	int start = Integer.valueOf(getRequest().getParameter("start"));
	int limit = Integer.valueOf(getRequest().getParameter("limit"));
	page.setStart(++start);
	page.setLimit(limit = limit == 0 ? 20 : limit);
	page.addPageInfo();
	page = userAddressService.findUserAddressByPage(page);

	outJsonString(JsonUtils.fromObject(page, new String[] { "createTime", "modifyTime", "new", }));
	return SUCCESS;

    }

    public String findAllUserAddress() throws Exception {
	page = new Page();
	// String searchField = getRequest().getParameter("searchField");
	String userName = getRequest().getParameter("searchValue");
	List result = userAddressService.findAllUserAddress(userName);
	page.setRoot(result);
	page.setLimit(result.size());
	JsonConfig config = new JsonConfig();
	outJsonString(JsonUtils.fromObject(page, new String[] { "createTime", "modifyTime", "new", }));
	return SUCCESS;
    }

    public String publish() {
	try {
	    if (StringUtils.isNotEmpty(ids)) {
		List idList = ConvertUtil.convertIdList(ids);
		userAddressService.publish(idList,BaseEntity.STATUS_PUBLISH_YES);
		List publishList = userAddressService.findUserAddressByPublish();
		boolean flag = createHtml(publishList);
		if(!flag){
		    outJsonString(RESULT_FAIL);
		    return SUCCESS;
		}
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String recall() {
	try {
	    if (StringUtils.isNotEmpty(ids)) {
		List idList = ConvertUtil.convertIdList(ids);
		userAddressService.publish(idList,BaseEntity.STATUS_PUBLISH_NO);
		
		List publishList = userAddressService.findUserAddressByPublish();
		boolean flag = createHtml(publishList);
		if(!flag){
		    outJsonString(RESULT_FAIL);
		    return SUCCESS;
		}
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    private boolean createHtml(List result) {
	Map contents = new HashMap();
	contents.put("result", result);
	return FreemarkerUtil.createHtml("index-txl.ftl", contents, WEB_SITE_PATH, "index-txl.htm");
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

    public boolean isSuccess() {
	return success;
    }

    public void setSuccess(boolean success) {
	this.success = success;
    }

    public UserAddress getUserAddress() {
	return userAddress;
    }

    public void setUserAddress(UserAddress userAddress) {
	this.userAddress = userAddress;
    }

    public Long getUerAddressId() {
	return uerAddressId;
    }

    public void setUerAddressId(Long uerAddressId) {
	this.uerAddressId = uerAddressId;
    }

    public void setUserAddressService(UserAddressService userAddressService) {
	this.userAddressService = userAddressService;
    }

    public void setIds(String ids) {
	this.ids = ids;
    }

    public String getIds() {
	return ids;
    }
}
